package com.webbei.Backstage.action;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.webbei.Backstage.service.BackstateSer;

public class InsertProductAction {
	private String productName;
	 private BackstateSer bao;
	 private int productSize;
     private String content;
     
	public String execute(){
		System.out.println("content..."+content);
		Map<String,Object> m=new HashMap<String,Object>();
		m.put("productName",productName);
		m.put("productSize",productSize);
		m.put("productDesc", content);
		System.out.println(m);
		int i=bao.insertNewProduct(m);
		if(i==1){
			String path=ServletActionContext.getServletContext().getRealPath("/");
			productName=ChinesToPinYinUtils.getPingYin(productName);
			System.out.println("new product pinyin name:"+productName);
			path=path+"img/resources/"+productName;
			File file=new File(path);
			if(!file.exists()){
				System.out.println("the path:"+path+",not exists,so create");
				file.mkdir();
			}
			try {
				ServletActionContext.getResponse().getWriter().write("success");
				ServletActionContext.getResponse().setContentType("text/html");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			try {
				ServletActionContext.getResponse().getWriter().write("fail");
				ServletActionContext.getResponse().setContentType("text/html");
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return null;
	}
	
	
	public int getProductSize() {
		return productSize;
	}


	public void setProductSize(int productSize) {
		this.productSize = productSize;
	}


	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public BackstateSer getBao() {
		return bao;
	}

	public void setBao(BackstateSer bao) {
		this.bao = bao;
	}


	public String getContent() {
		return content;
	}


	public void setContent(String content) {
		this.content = content;
	}
	
}
